<template>
	<el-dialog
		title="选择机构"
		:width="300"
		destroy-on-close
        v-model="visible"
		@closed="close"
	>
 <el-container>
    <el-main class="nopadding">
        <el-input v-model="groupFilterText" placeholder="名称" />
        <el-tree ref="tree" class="menu" node-key="id" :data="group" :current-node-key="''"
            :highlight-current="true" :expand-on-click-node="false" default-expand-all="true" :props="defaultProps"
            :filter-node-method="groupFilterNode" @node-click="groupClick">
		</el-tree>
    </el-main>
</el-container>
</el-dialog>
<save-dialog v-if="dialogSave" ref="saveDialog" @fatherMethod="close" @success="handleSuccess"
		@closed="dialogSave = false"></save-dialog>
</template>
<script>
import publicjs from "@/api/model/publicjs";
import saveDialog from "./save";
export default {
    name: "orgSel",
	components: {
		saveDialog,
	},
    data() {
		return {
            group:[],
            visible: true,
            groupFilterText: "",
			dialogSave: false,
			orgCode:"",
			orgName:"",
			defaultProps:{
				children:"children",
				label:"label",
				disabled:function(data,node) {
					if(data.children&&data.children.length>0){
						return false;
					}else{
						return true;
					}
				}
			},
        }
	},
    watch: {
		groupFilterText(val) {
			this.$refs.tree.filter(val);
		},
	},
    methods:{
        //加载树数据getGroup
		async getTree() {
			this.showGrouploading = true;
			var head = new publicjs.head();
			//publicjs.initHead(head, "query");
			publicjs.initHead(head, "orgtreenew");
			var ed = new publicjs.entitydata(head);
			var eo = new publicjs.entityobj();
			eo.AttrList = {
				Name: "Organize",
			};
			eo.ValueList = {
				Code: "id",
				Name: "label",
				ParentCode: "parentId",
				State: "State",
			};
			eo.QueryList = { State: "1" };
			eo.OrderList = { Code: "asc" };
			ed.Data.Content.ObjectList.Object.push(eo);
			try {
				var returnValueStr = await this.$API.auth.objProcess.post(ed);
				//alert(returnValueStr)
				var returnValue = JSON.parse(returnValueStr);
				let result = JSON.parse(returnValue.Result.Content);
				console.log(result);
				// eslint-disable-next-line no-debugger
				debugger;
				// var allNode = { id: "", label: "所有" };
				// result.unshift(allNode);
				this.group = result;
				this.showGrouploading = false;
			} catch (error) {
				this.showGrouploading = false;
				this.$alert(error.message, "提示", { type: "error" });
				return false;
			}
		},
        groupFilterNode(value, data) {
			if (!value) return true;
			return data.label.indexOf(value) !== -1;
		},
        //树点击事件
		groupClick(data) {
			// var params = {
			// 	groupId: data.id,
			// };
			// eslint-disable-next-line no-debugger
			//debugger
			this.orgCode = data.id;
			this.orgName = data.label;
			this.dialogSave = true;
			this.$nextTick(() => {
				this.$refs.saveDialog.open("Add", this.orgCode, this.orgName);
				//this.visible=false;
			});
		},
		close(){
			this.visible=false;
			this.$emit('closed');
			this.$emit('fatherMethod');
		},
    },
	async mounted() {
		await this.getTree();
	},
}
</script>